package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.FunctionUsers;
import top.jnaw.jee.platform.model.Mediation;
import top.jnaw.jee.platform.model.Users;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Shiros;
import top.jnaw.jee.utils.Strings;
import top.jnaw.jee.utils.wechat.Wechats;

public class ApprovalArbitrationController extends Controller {

  private static final String WECHAT_STATE = "arbitration";
  private static final String WECHAT_APP_URL =
      Consts.DOMAIN + "wechat/index.html?data=%s";

  public void index() {

    Wechats.redirect(this, WECHAT_STATE, WECHAT_APP_URL);
  }

  public void add() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int mid = Strings.getInt(req, "mid", Consts.DEFAULT_BAD_ID);
    if (mid > 0) {
      result = Mediation.add(mid);
    }
    renderJson(result.toJson());
  }

  public void query() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    if (id > 0) {
      result = Mediation.queryArb(id);
    }
    renderJson(result.toJson());
  }

  public void update() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final String type = Strings.getString(req, "type", "");
    final JSONArray accessory = Strings.getJSONArray(req, "accessory", null);
    if (id > 0 && StrKit.notBlank(type)) {
      result = Mediation.updateStage(id, type, accessory);
    }
    renderJson(result.toJson());
  }

  public void check() {
    ModelResult result = new ModelResult(Result.kUnauthorized);
    Users user = Users.getByUsername(Shiros.getUser());
    int uid = (user != null) ? user.getId() : Consts.DEFAULT_BAD_ID;
    if (uid > 0) {
      JSONObject object = new JSONObject();
      object.put("is_auth", FunctionUsers.check(7, uid));
      result.setResult(Result.kSuccess);
      result.setObject(object);
    }
    renderJson(result.toJson());
  }

  public void send() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final String type = Strings.getString(req, "type", "");
    final String content = Strings.getString(req, "content", "");
    final String date = Strings.getString(req, "date", "");
    final String address = Strings.getString(req, "address", "");
    Users user = Users.getByUsername(Shiros.getUser());
    int uid = (user != null) ? user.getId() : Consts.DEFAULT_BAD_ID;
    if (uid > 0) {
      result = Mediation.send(id, type, content, date, address);
    }
    renderJson(result.toJson());
  }
}
